package com.windchime.blog.controller.admin;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.windchime.blog.entity.Blog;
import com.windchime.blog.entity.vo.BlogSearch;
import com.windchime.blog.entity.vo.TypeOrTagVo;
import com.windchime.blog.entity.vo.status.EditAppreciate;
import com.windchime.blog.entity.vo.status.EditCommentable;
import com.windchime.blog.entity.vo.status.EditPublished;
import com.windchime.blog.result.R;
import com.windchime.blog.service.BlogService;
import com.windchime.blog.utils.StringSplitUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 博客表 前端控制器
 * </p>
 *
 * @author WindChime
 * @since 2021-08-03
 */
@RestController
@RequestMapping("/admin/blog")
public class AdminBlogController {

    @Autowired
    private BlogService blogService;

    /**
     * 分页查询带条件
     * @param page 当前页
     * @param limit 当前页条目数
     * @param blogSearch 条件
     * @return blogs
     */
    @GetMapping("/selectBlogList")
    public R selectBlogList(Integer page, Integer limit, BlogSearch blogSearch) {
        PageInfo<Blog> blogs = blogService.selectBlogList(page, limit, blogSearch);
        return R.ok().code(0).msg("").count(blogs.getTotal()).data("blogs", blogs.getList());
    }

    /**
     * 添加博客
     * @param blog 博客
     * @return true
     */
    @PostMapping("/saveBlog")
    public R saveBlog(Blog blog) {
        boolean b = blogService.saveBlog(blog);
        if (b) {
            return R.ok().msg("添加成功");
        } else {
            return R.error().msg("添加失败");
        }
    }

    /**
     * 修改博客
     * @param blog 博客
     * @return true
     */
    @PostMapping("/editBlog")
    public R editBlog(Blog blog) {

        boolean b = blogService.updateById(blog);
        if (b) {
            return R.ok().msg("修改成功");
        } else {
            return R.error().msg("修改失败");
        }
    }

    /**
     * 删除博客
     * @param id 博客ID
     * @return true
     */
    @GetMapping("/deleteBlog/{id}")
    public R deleteBlog(@PathVariable Long id) {
        boolean b = blogService.removeById(id);
        if (b) {
            return R.ok().msg("删除成功");
        } else {
            return R.error().msg("删除失败");
        }
    }

    /**
     * 批量删除
     * @param ids 博客ID集合
     * @return true
     */
    @GetMapping("/deleteBlogs/{ids}")
    public R deleteBlog(@PathVariable String ids) {
        List<Long> idList = StringSplitUtil.split(ids, ",");
        boolean b = blogService.removeByIds(idList);
        if (b) {
            return R.ok().msg("删除成功");
        } else {
            return R.error().msg("删除失败");
        }
    }

    /**
     * 修改发布状态
     * @param editPublished vo对象
     * @return true
     */
    @PostMapping("/editPublished")
    public R editPublished(EditPublished editPublished) {
        boolean b = blogService.editPublished(editPublished);
        if (b) {
            return R.ok().msg("修改成功");
        } else {
            return R.error().msg("修改失败");
        }
    }

    /**
     * 修改赞赏状态
     * @param editAppreciate vo对象
     * @return true
     */
    @PostMapping("/editAppreciate")
    public R editAppreciate(EditAppreciate editAppreciate) {
        boolean b = blogService.editAppreciate(editAppreciate);
        if (b) {
            return R.ok().msg("修改成功");
        } else {
            return R.error().msg("修改失败");
        }
    }

    /**
     * 修改评论状态
     * @param editCommentable vo对象
     * @return true
     */
    @PostMapping("/editCommentable")
    public R editCommentable(EditCommentable editCommentable) {
        boolean b = blogService.editCommentable(editCommentable);
        if (b) {
            return R.ok().msg("修改成功");
        } else {
            return R.error().msg("修改失败");
        }
    }

    /**
     * 选择哪篇博客发表评论--评论后台用
     * @return list
     */
    @PostMapping("/selectBlog")
    public R selectBlog() {
        List<Blog> blogs = blogService.list(new QueryWrapper<Blog>().eq("commentable", true).eq("published", true));
        List<TypeOrTagVo> typeOrTagVos = new ArrayList<>();
        for (Blog blog : blogs) {
            typeOrTagVos.add(new TypeOrTagVo(blog.getTitle(), blog.getId()));
        }
        return R.ok().data("blogs", typeOrTagVos);
    }

}

